package web;

import utilJdbc.jdbc;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //System.out.println("woshi超链接过来的");
        //req.getRequestDispatcher("/login.html").forward(req,resp);
        HttpSession session = req.getSession(false);
        if(session==null) {
            req.getRequestDispatcher("/login.html").forward(req,resp);
            return;
        }
        session.removeAttribute("userName");
        //System.out.println(session.getAttribute("userName"));
        req.getRequestDispatcher("/login.html").forward(req,resp);

    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("UserServlet---》");
        jdbc jd = new jdbc();
        String userName = req.getParameter("userName");
        String pwd = jd.yanzheng(userName);
        String userPassword = req.getParameter("userPassword");
        //System.out.println(pwd);
        if(userPassword.equals(pwd)){
            HttpSession session = req.getSession();
            session.setAttribute("userName",userName);
           // System.out.println(session.getAttribute("userName"));
           // req.getRequestDispatcher("/sucess.jsp").forward(req,resp);
            resp.sendRedirect("/sucess.jsp");
        } else{//req.getRequestDispatcher("/failed.html").forward(req,resp);
            resp.sendRedirect("/failed.html");
        }


        //System.out.println(userName+","+userPassword);
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        //System.out.println("我来看了");
    }
}
